package com.cskaoyan.javase.network._1udp._v4;

import com.cskaoyan.javase.network.util.UDPNetworkUtils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * 接收任务的线程需要执行的任务
 *
 * @since 14:41
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class ReceiveMsgTask implements Runnable {
    private DatagramSocket socket;

    public ReceiveMsgTask(DatagramSocket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        while (true) {
            DatagramPacket receiverPacket = UDPNetworkUtils.getReceiverPacket();
            try {
                socket.receive(receiverPacket);
                System.out.println(UDPNetworkUtils.parsePacket(receiverPacket));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
